import { FormGroup } from '@angular/forms'
import { NzMessageService } from 'ng-zorro-antd'
import { I18NService } from '@core'

export class NewPageBaseClass {

  validateForm: FormGroup

  constructor(
    public msg: NzMessageService,
    public i18n: I18NService,
  ) {}

  checkValid() {
    if (this.validateForm.valid) {
      this.msg.success(this.trans('GENERAL.NOTIFICATION.VALIDATION_SUCCESS'))
      return true
    } else {
      this.makeAllControlDirty(this.validateForm)
      this.msg.error(this.trans('GENERAL.NOTIFICATION.VALIDATION_FAIL'))
      return false
    }
  }

  makeAllControlDirty(validateForm) {
    for (const key in validateForm.controls) {
      validateForm.controls[key].markAsDirty()
      validateForm.controls[key].updateValueAndValidity()
    }
  }

  trans(key: string, params?: Object): string {
    return this.i18n.fanyi(key, params)
  }
}
